# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\PyInstaller\utils\win32\winutils.py
"""
Utils for Windows platform.
"""
__all__ = ["get_windows_dir"]
import os, sys, PyInstaller.log as logging

logger = logging.getLogger(__name__)


def get_windows_dir():
    r"""
    Return the Windows directory e.g. C:\Windows.
    """
    from ... import compat

    windir = compat.win32api.GetWindowsDirectory()
    if not windir:
        raise SystemExit("Error: Can not determine your Windows directory")
    return windir


def get_system_path():
    """
    Return the path that Windows will search for dlls.
    """
    from ... import compat

    _bpath = []
    sys_dir = compat.win32api.GetSystemDirectory()
    _bpath = [sys_dir, get_windows_dir()]
    _bpath.extend(compat.getenv("PATH", "").split(os.pathsep))
    return _bpath


def extend_system_path(paths):
    """
    Add new paths at the beginning of environment variable PATH.
    
    Some hooks might extend PATH where PyInstaller should look for dlls.
    """
    from ... import compat

    old_PATH = compat.getenv("PATH", "")
    paths.append(old_PATH)
    new_PATH = os.pathsep.join(paths)
    compat.setenv("PATH", new_PATH)


def import_pywin32_module(module_name, _is_venv=None):
    """
    Import and return the PyWin32 module with the passed name.
    
    When imported, the `pywintypes` and `pythoncom` modules both internally
    import dynamic libraries (e.g., `pywintypes.py` imports `pywintypes34.dll`
    under Python 3.4). The Anaconda Python distribution for Windows installs
    these libraries to non-standard directories, resulting in
    `"ImportError: No system module 'pywintypes' (pywintypes34.dll)"`
    exceptions. This function catches these exceptions, searches for these
    libraries, adds their directories to `sys.path`, and retries.
    
    Parameters
    ----------
    module_name : str
        Fully-qualified name of this module.
    _is_venv: bool
        Internal paramter used by compat.py, to prevent circular import. If None
        (the default), compat is imported and comapt.is_venv ist used. If not
        None, it is assumed to be called from compat and the value to be the same
        as compat.is_venv.
    
    Returns
    ----------
    types.ModuleType
        The desired module.
    """
    module = None
    try:
        module = __import__(name=module_name, globals={}, locals={}, fromlist=[""])
    except ImportError as exc:
        if str(exc).startswith("No system module"):
            is_sys_frozen = hasattr(sys, "frozen")
            sys_frozen = getattr(sys, "frozen", None)
            sys.frozen = "|_|GLYH@CK"
            if _is_venv is None:
                from ... import compat

                _is_venv = compat.is_venv
            if _is_venv:
                sys_paths = sys.path
            else:
                import site

                sys_paths = site.getsitepackages()
            for sys_path in sys_paths:
                pywin32_dll_dir = os.path.join(sys_path, "pywin32_system32")
                if os.path.isdir(pywin32_dll_dir):
                    sys.path.append(pywin32_dll_dir)
                    try:
                        module = __import__(
                            name=module_name, globals={}, locals={}, fromlist=[""]
                        )
                        break
                    except ImportError:
                        pass

            if is_sys_frozen:
                sys.frozen = sys_frozen
            else:
                del sys.frozen
            if module is None:
                raise

    return module


def convert_dll_name_to_str(dll_name):
    """
    Convert dll names from 'bytes' to 'str'.
    
    Latest pefile returns type 'bytes'.
    :param dll_name:
    :return:
    """
    from ...compat import is_py3

    if is_py3:
        if isinstance(dll_name, bytes):
            return str(dll_name, encoding="UTF-8")
        return dll_name
